/**
 * Created by qunxing.du on 2016/8/18.
 * Copyright 2016 汉威.智慧环保事业部 Limited.
 * All rights reserved.
 */
package com.hw.one.collect.pollution.server.data;

import com.hw.one.collect.data.DataService;
import com.hw.one.collect.model.DataType;
import com.hw.one.collect.util.ConvertUtil;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import org.apache.log4j.Logger;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 污染源数据服务
 */
@Component
public class PollutionDataService extends DataService {
    private static Logger log = Logger.getLogger(PollutionDataService.class);
    @Resource
    private MongoTemplate pollutionMongoTemplate;

    /**
     * 插入企业基本信息
     * @param table
     * @param data
     */
    public void insertEntBasicInfo(String table,Object data) throws Exception{
        List<Map> list = ConvertUtil.toList(data);
        for (Map map : list) {
            String psCode = (String) map.get("PSCode");
            Query query = Query.query(Criteria.where("PSCode").is(psCode));

            BasicDBObject basicDBObject = new BasicDBObject();
            basicDBObject.putAll(map);
            Update update =  Update.fromDBObject(basicDBObject,new String[]{});
            pollutionMongoTemplate.upsert(query,update,Map.class,table);
        }
        log.info("企业基本信息更新插入成功");
    }

    /**
     * 插入排放口基本信息
     * @param table
     * @param data
     */
    public void insertVent(String table,Object data) throws Exception{
        List<Map> list = ConvertUtil.toList(data);
        for (Map map : list) {
            String psCode = (String) map.get("PSCode");
            String outputCode = (String) map.get("OutputCode");
            Query query = Query.query(Criteria.where("PSCode").is(psCode).and("OutputCode").is(outputCode));

            BasicDBObject basicDBObject = new BasicDBObject();
            basicDBObject.putAll(map);
            Update update =  Update.fromDBObject(basicDBObject,new String[]{});

            pollutionMongoTemplate.upsert(query,update,Map.class,table);
        }
        log.info("排放口信息更新插入成功");
    }

    /**
     * 插入污染物设置信息
     * @param table
     * @param data
     */
    public void insertPollutantSetInfo(String table,Object data) throws Exception{
        List<Map> list = ConvertUtil.toList(data);
        for (Map map : list) {
            String psCode = (String) map.get("PSCode");
            String outputCode = (String) map.get("OutputCode");
            String pollutantCode = (String) map.get("PollutantCode");
            Query query = Query.query(Criteria.where("PSCode").is(psCode).and("OutputCode").is(outputCode)
            .and("PollutantCode").is(pollutantCode));

            BasicDBObject basicDBObject = new BasicDBObject();
            basicDBObject.putAll(map);
            Update update =  Update.fromDBObject(basicDBObject,new String[]{});

            pollutionMongoTemplate.upsert(query,update,Map.class,table);
        }
        log.info("污染物设置信息更新插入成功");
    }
    /**
     * 插入采集数据
     * @param table
     * @param data
     */
    public void insertExhaust(String table,Object data) throws Exception{

        pollutionMongoTemplate.insert(getBasicDBList(data),table);
        log.info("废气数据插入成功");
    }

    public void insertTransFormedExhaust(String table,Object data) throws Exception{
        List<Map> list =  (List<Map>)data;
        for (Map<String,Object> map : list){
            String psCode = (String) map.get("PSCode");
            String outputCode = (String) map.get("OutputCode");

            //查询企业基本信息
            Map<String,Object> entBasicInfo = queryEntBasicIfo(psCode);
            //查询排放口基本信息
            Map<String,Object> vent = queryVent(psCode,outputCode);

            //存放查询的企业信息和排放口信息
            if(entBasicInfo == null || vent == null){
                continue;
            }
            map.putAll(entBasicInfo);
            //防止经纬度覆盖污染源的经纬度
            vent.put("OP_Longitude",vent.get("Longitude"));//排放口经度 ，防止与企业信息中经纬度重名覆盖
            vent.put("OP_Latitude",vent.get("Latitude"));//排放口维度
            vent.remove("Longitude");
            vent.remove("Latitude");
            map.putAll(vent);
            map.remove("_id");//移除_id值，防止重复
            pollutionMongoTemplate.insert(map,table);
        }
        log.info("废气数据转换后插入成功");
    }

    /**
     * 查询企业基本信息
     * @param psCode
     * @return
     */
    public Map<String,Object> queryEntBasicIfo(String psCode){
        DBObject  entBasicInfoQuery = new BasicDBObject();
        entBasicInfoQuery.put("PSCode",psCode);
        DBObject entBasicInfoFields = new BasicDBObject();
        entBasicInfoFields.put("PSName",true);
        entBasicInfoFields.put("RegionCode",true);
        entBasicInfoFields.put("RegionName",true);
        entBasicInfoFields.put("IndustryTypeCode",true);
        entBasicInfoFields.put("Longitude",true);
        entBasicInfoFields.put("Latitude",true);
        Map<String,Object> entBasicInfo = pollutionMongoTemplate.findOne(new BasicQuery(entBasicInfoQuery,entBasicInfoFields),Map.class,DataType.ENT_BASIC_INFO.name());
        return entBasicInfo;
    }

    /**
     * 查询排放口基本信息
     * @param psCode
     * @param outputCode
     * @return
     */
    public Map<String,Object> queryVent(String psCode,String outputCode){
        DBObject  ventQuery = new BasicDBObject();
        ventQuery.put("PSCode",psCode);
        ventQuery.put("OutputCode",outputCode);
        DBObject ventFields = new BasicDBObject();
        ventFields.put("OutputName",true);
        ventFields.put("OutputCode",true);
        ventFields.put("Longitude",true);
        ventFields.put("Latitude",true);
        Map<String,Object> vent = pollutionMongoTemplate.findOne(new BasicQuery(ventQuery,ventFields),Map.class,DataType.VENT.name());
        return vent;
    }
}
